// pkg_shop/pages/shopOrders/shopOrders.js
import request from '../../../utils/http.js'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    swiperHeight: 0,
    currentTab: 0, //0:全部 1待付款  2：待发货  3：待收货  4：已完成
    tabList: ["全部", "待发放 ", "已发放 ", "退款关闭"],




    dianPuId: 0,
    leiXing: 1, //类型 1全部，2待发放，3已发放，4退款


    indexPage: 1, //当前页码
    isMaxPageNow: false, //总页数

    orderList: [],

    allOrderList: [],
    allMaxPage: false,
    allIndexPage: 1,


    waitOrderList: [],
    waitMaxPage: false,
    waitIndexPage: 1,

    getMoneyOrderList: [],
    getMoneyMaxPage: false,
    getMoneyIndexPage: 1,

    overOrderList: [],
    overMaxPage: false,
    overIndexPage: 1,

    isYuanGong: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setData({
      dianPuId: wx.getStorageSync(app.globalData.SHOPID),
    })

    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          swiperHeight: res.windowHeight - 64
        });
      }
    });

    that.getOrderList(1)
    that.getOrderList(2)
    that.getOrderList(3)
    that.getOrderList(4)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {


  },


  //产品分组切换
  onTabsChange(e) {
    let index = e.detail.current == undefined ? e.detail.value : e.detail.current

    if (this.data.currentTab == index) {
      return false;
    } else {
      this.setData({
        currentTab: index
      })
    }
  },



  getMoreSelect: function (e) {
    var index = e.currentTarget.dataset.index;
    this.getOrderList(index);
  },


  
  getOrderList: function (index) {
    var that = this

    var isMaxPage = index == 1 ? that.data.allMaxPage : index == 2 ? that.data.waitMaxPage : index == 3 ? that.data.getMoneyMaxPage : that.data.overMaxPage
    var indexPage = index == 1 ? that.data.allIndexPage : index == 2 ? that.data.waitIndexPage : index == 3 ? that.data.getMoneyIndexPage : that.data.overIndexPage

    // console.log(isMaxPage)

    if (isMaxPage) {
      let str = index == 1 ? "已获取所有数据" : "已获取近三个月所有数据"
      console.log(str)
      wx.showToast({
        title: str, // 提示的内容
        icon: "none", // 图标，默认success
        duration: 2000, // 提示的延迟时间，默认1500
        mask: false, // 是否显示透明蒙层，防止触摸穿透
      })
    } else {

      wx.showLoading({
        title: '加载中...',
        mask: true
      })

      let data = {
        dianPuId: that.data.dianPuId,
        leiXing: index,
        index: indexPage,

      }
      request.get('dianPu/xiaoShouDingDanPage', data, res => {

        // console.log("indexPage:"+indexPage+"--" + JSON.stringify(res.data))

        that.setData({
          isYuanGong: res.data.isYuanGong,
        })

        if(index==1){
          that.setData({
            allOrderList: indexPage == 1 ? res.data.list : that.data.allOrderList.concat(res.data.list),
            allMaxPage: res.data.totalPage == indexPage ? true : false,
            allIndexPage: res.data.totalPage > indexPage ? indexPage + 1 : indexPage,
          })
        } else if(index==2){
          that.setData({
            waitOrderList: indexPage == 1 ? res.data.list : that.data.waitOrderList.concat(res.data.list),
            waitMaxPage: res.data.totalPage == indexPage ? true : false,
            waitIndexPage: res.data.totalPage > indexPage ? indexPage + 1 : indexPage,
          })
        }else if(index==3){
          that.setData({
            getMoneyOrderList: indexPage == 1 ? res.data.list : that.data.getMoneyOrderList.concat(res.data.list),
            getMoneyMaxPage: res.data.totalPage == indexPage ? true : false,
            getMoneyIndexPage: res.data.totalPage > indexPage ? indexPage + 1 : indexPage,
          })
        }else if(index==4){
          that.setData({
            overOrderList: indexPage == 1 ? res.data.list : that.data.overOrderList.concat(res.data.list),
            overMaxPage: res.data.totalPage == indexPage ? true : false,
            overIndexPage: res.data.totalPage > indexPage ? indexPage + 1 : indexPage,
          })
        }

        wx.hideLoading()
      }, err => {
        wx.showToast({
          title: '查询失败,请联系技术人员', // 提示的内容
          icon: "none", // 图标，默认success
          duration: 1000, // 提示的延迟时间，默认1500
          mask: false, // 是否显示透明蒙层，防止触摸穿透
        })
        wx.hideLoading()
      })
    }
  },


  //订单详情
  toOrderInfo: function (e) {
    var id = e.currentTarget.dataset.id;
    var v= e.currentTarget.dataset.v;
    wx.navigateTo({
      url: '/pkg_wo/pages/orderInfo/orderInfo?orderFormId=' + id + '&pathSign=2'+'&v='+v,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})